import {
    GetBaseAddress,
    GetStoreAddressListWithPage,
    AddStoreAddress,
    GetStoreAddress,
    UpdateStoreAddress,
    DeleteStoreAddress
} from '@/services/setting/address';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'address',

    state: {
        addressList: [],
        addressListPage: [],
        addressbyId: {}
    },

    effects: {

        // 获取三级地址
        *GetBaseAddress({payload, callBack}, {call, put}) {
            const data = yield call(GetBaseAddress, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                yield put({
                    type: 'saveGetBaseAddress',
                    payload: {
                        dataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },

        //获取地址库列表
        *GetStoreAddressListWithPage({payload, callBack}, {call, put}) {
            const data = yield call(GetStoreAddressListWithPage, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                yield put({
                    type: 'saveGetStoreAddressListWithPage',
                    payload: {
                        dataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },

        //ID获取
           *GetStoreAddress({payload, callBack}, {call, put}) {
            const data = yield call(GetStoreAddress, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                yield put({
                    type: 'saveGetStoreAddress',
                    payload: {
                        dataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },



        //新增
        *AddStoreAddress({payload, callBack}, {call, put}) {
            const data = yield call(AddStoreAddress, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                callBack && callBack.error(data)
              }
            })

          },


        //修改
        *UpdateStoreAddress({payload, callBack}, {call, put}) {
            const data = yield call(UpdateStoreAddress, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                callBack && callBack.error(data)
              }
            })

          },

        //删除
        *DeleteStoreAddress({payload, callBack}, {call, put}) {
            const data = yield call(DeleteStoreAddress, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                callBack && callBack.error(data)
              }
            })

          },

    },

    reducers: {
        saveGetBaseAddress(state, {payload}) {
            return {
                ...state,
                addressList: payload.dataList,
            };
        },

        saveGetStoreAddressListWithPage(state, {payload}) {
            return {
                ...state,
                addressListPage: payload.dataList,
            };
        },

        saveGetStoreAddress(state, {payload}) {
            return {
                ...state,
                addressbyId: payload.dataList,
            };
        },
    }
}
